package actions;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.VenueModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import db.DBModel;

@WebServlet
public class UpdateEventAction extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response){		
		try {
			int eventId = Integer.parseInt(request.getParameter("eventId"));
			int maxCapacity;
			
			String eventName=request.getParameter("eventName");
			int minCapacity=Integer.parseInt(request.getParameter("minCapacity"));
			String maxCapacityString=request.getParameter("maxCapacity");
			try{
				maxCapacity=Integer.parseInt(maxCapacityString);
			}
			catch(Exception e){
				maxCapacity=-1;
			}
			Timestamp t = getTimestamp(request.getParameter("date").trim(),request.getParameter("time").trim());
			String desc=request.getParameter("desc");
			int venue=Integer.parseInt(request.getParameter("venue"));
			
			DBModel db = new DBModel();
			
			//Check to see if venue capacity is big enough for this event
			if(db.validVenueCapacity(venue,Math.max(minCapacity,maxCapacity))){
				//Insert event
				db.updateEvent(eventName, minCapacity, maxCapacity, t, desc, venue, eventId);
				
				//Commit and close db
				db.commit();
	    		db.close();
	    		
	            //response.sendRedirect("Event?id="+eventId);
				if(request.isUserInRole("admin")){
					return mapping.findForward("adminSuccess");
				}
				else{
					return new ActionForward(mapping.findForward("success").getPath()+"?id="+eventId);
				}
			}
			else{
				Map<String,String> errorMessages=new HashMap<String,String>();
				errorMessages.put("venue", "The venue you selected does not support your event!");
				request.setAttribute("errorMessages", errorMessages);
				return mapping.findForward("invalid");
			}

		} catch (Exception e){
			e.printStackTrace();
			request.setAttribute("error", e.toString());
			return mapping.findForward("fail");
		}	
	}
	
	@SuppressWarnings("deprecation")
	public Timestamp getTimestamp(String date, String time){
            String[] datet = date.split("/");
            int month=Integer.parseInt(datet[0])-1;
            int day=Integer.parseInt(datet[1]);
            int year=Integer.parseInt(datet[2])-1900;
            
    		String[] timet = time.split(":");
            int hour=Integer.parseInt(timet[0]);
            int minute=Integer.parseInt(timet[1]);

        	return new java.sql.Timestamp(year, month, day, hour, minute, 0, 0);
	}
	
	private Collection getVenueListCollection(ArrayList<VenueModel> venues, int venueId, String venueName){
		Collection venueList=new Vector(venues.size());
		//Adds the already selected venue first
		venueList.add(new LabelValueBean(venueName, String.valueOf(venueId)));
		for(int i=0;i<venues.size();i++){
			if(venues.get(i).getId()!=venueId){
				venueList.add( new LabelValueBean(venues.get(i).getName(),String.valueOf(venues.get(i).getId())) );
			}
		}
		return venueList;
	}
}
